var $editDialog;
var $mapDialog;
var getEditDialog;
require(['grid', 'topBar', 'layout','validator'], function () {

	   $('#top').topBar({
		left : [ {
			text : '所属机构',
			name : 'organId',
			option : {
				url : '/baseOrgan/getOrganTree.htm',
				name : 'organName',
				idKey : 'organId',
				pIdKey : 'parentOrganId'
			},
			width : 150,
			type : 'text-tree'
		}, {
			text : '停车场名称',
			name : 'parkingLotName',
			width : 80,
			type : 'text'
		}, {
			text : '查询',
			type : 'btn',
			click : function() {
				search();
			}
		}],
		right : [ {
			text : '导出',
			type : 'export-btn',
			authority : '',
			item : [ {
				text : 'Excel导出',
				click : function() {
					exportFile('Excel');
				}
			}, {
				text : 'PDF导出',
				click : function() {
					exportFile('Pdf');
				}
			} ]
		}, {
			text : '+添加',
			type : 'btn',
			width : '698',
			authority : '',
			click : function() {
				getEditDialog(0,'添加','');
			}
		} ]
	});

    $('body').layout();
    
    getEditDialog= function(edit,title,data){
    	if(!$editDialog){
		        $editDialog = $('<div>').dialog({
		            url: './master/setting/sysParkingInfoEdit.htm',
			        height: 600,
			        width: 720,
			        close:function(){
			        	$editDialog.dialog('exec', 'reset');
			        },
		            buttons: [
		          	{
						text : '确定',
						css : 'mr10',
						click : function() {
							 $editDialog.dialog('exec', 'saveOrUpdate',function(){
			         		    	$editDialog.dialog('close');
			                    	search();
			        		    });
						}
					}, {
						text : '关闭',
						css : 'btn-cancel',
						click : function() {
							$editDialog.dialog('close');
						}
					}
		          ]
		    });	
    	}
    	
    	$editDialog.dialog('setOption','title',title);
        
    	//添加
    	if(edit==0){
    		$editDialog.dialog('setOption','showBtn',[0,1]);
            $editDialog.dialog('exec', 'reset');
    		$editDialog.dialog('open');
    	}
    	//查看地图
    	else if(edit==1){
    		if(!$mapDialog){
	        		$mapDialog = $('<div>').dialog({
	        			title:'位置',
			            url: './master/setting/sysParkingInfoMap.htm',
				        height: 600,
				        width: 720,
			            buttons: [{
							text : '关闭',
							css : 'btn-cancel',
							click : function() {
								$mapDialog.dialog('close');
							}
						}
			          ]
			    });
    		}
    		$mapDialog.dialog('open');
    		$mapDialog.dialog('exec', 'loadMap',data.parkingId);
		}
    	//编辑
    	else if(edit==2){
    		$editDialog.dialog('setOption','showBtn',[0,1]);
    		if(data.wthrStart=='是'){
    			data.wthrStart='1';
    		}
    		else{
    			data.wthrStart='0';
    		}
    		$editDialog.dialog('exec', 'reload',data);
    		$editDialog.dialog('open');
		}    	
    } 
    
    function exportFile(suffix){
		if($('#grid').getGridParam("reccount")>0){
			var $postData=$("#top").parent().getValues();
            var url = "/sysParking/exportParkingInfo"+suffix+".htm";
            $.exportFile(url,$postData);
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }
    
    $('#grid').grid({
    	multiselect: false,
        url: "/sysParking/searchParkingInfoPage.htm",
        colModel: [
            {name: "parkingId", index: "parkingId", label: 'parkingId', key: true, hidden: true},
            {name: "provinceId", index: "provinceId", label: 'provinceId', hidden: true},
            {name: "cityId", index: "cityId", label: 'cityId', hidden: true},
            {name: "parkingLotCd", index: "parkingLotCd", label: '编号', width: 100},
            {name: "parkingLotName", index: "parkingLotName", label: '名称', width: 100},
            {name: "organId", index: "organId", label: 'organId', hidden: true},
            {name: "organName", index: "organName", label: '所属机构', width: 100},
            {name: "parkingLotArea", index: "parkingLotArea", label: '面积（平米）', width: 100},
            {name: "parkingLotCapty", index: "parkingLotCapty", label: '容量（辆）', width: 100},
            {name: "remark", index: "remark", label: 'remark', hidden: true},
            {name: "wthrStart", index: "wthrStart", label: '是否始发站', width: 100},
            {name: "operate", width: 100, label: '操作', title: false,
                formatter: 'operArea',
                formatoptions: [{
                    text: '位置',
                    css:'operate-scan',
                    click: function (data) {
                    	 getEditDialog(1, '位置', data);
                    }
                },{
                    text: '编辑',
                    css:'operate-edit',
                    click: function (data) {
                    	 getEditDialog(2, '编辑', data);
                    }
                }, {
                    text: '删除',
                    css:'operate-delete',
                    click: function (data) {
                    	$.messager.confirm("是否删除","是否删除该模板信息？",function(r){
                			if(r){
                				$.ajaxExt({
                					url : "/sysParking/delBaseParkingInfo.htm",
                					data : {'parkingId' : data.parkingId}
                				}).done(function(data){
                					search();
                				});
                			}
                		});
                    }
                }]        
            }
        ]
    });

    function search(){
    	var $postData=$("#top").parent().getValues();
        $('#grid').grid('setGridParam', {
            datatype: "json",
            postData: $postData
        }).trigger("reloadGrid");
    }

    //进入页面自动查询
    search();
});